//
//  RMMTuiKuanViewController.m
//  Romomo
//
//  Created by puman on 2016/10/21.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMTuiKuanViewController.h"
#import "RMMCommonBottomView.h"
#import "RMMMineSetTableViewCell.h"
#import "RMMSelcetPickerView.h"
#import "RMMOrderTuiKuanFooterView.h"
#import "RMMOrderShuoMingTableViewCell.h"
#import "RMMTuiKuaiJieGuoView.h"
#import "RMMAboutMineViewController.h"

@interface RMMTuiKuanViewController ()<UITableViewDataSource, UITableViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource,UITextFieldDelegate,UITextViewDelegate>

@end

@implementation RMMTuiKuanViewController
{
    RMMCommonBottomView *_bottomView;
    RMMSelcetPickerView *_pickerView;
    RMMTuiKuaiJieGuoView *_tuiKuaiJieView;
    NSString *_yuanYin;
    UITableView *_tableView;
    NSArray *_nameArray;
    NSArray *_array;
    NSInteger _row;
    CGFloat _rowHeight;
    NSString *_stat;
    NSString *_totalPrcie;
    UITextView *_textView;
    NSDictionary *_dataDic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kCColor;
    if ([_type isEqualToString:@"申请退款"]) {
        self.navigationItem.title = @"申请退款";
        [self addViews];
    } else {
        self.navigationItem.title = @"结果详情";
        [self addTuiKuanDetailsViewWith:@"0"];
    }
    [self getTuiDanTotalPrice];
    // Do any additional setup after loading the view.
}
- (void)getTuiDanTotalPrice{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"order_id"] = _order_id;
    NSString *url = nil;
    if ([_type isEqualToString:@"申请退款"]) {
        url = GetReturnAtmUrl;
    } else {
        url = GetReturnOrderDetailUrl;
    }
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            if ([_type isEqualToString:@"申请退款"]) {
                _totalPrcie = [NSString stringWithFormat:@"%@",dataDic[@"data"][@"END_ATM"]];
            } else {
                _tuiKuaiJieView.dic = [RMMFileManager setValueForDic:dataDic[@"data"]];
            }
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        if (url == GetReturnAtmUrl) {
            [_tableView reloadData];
        }
    } error:^(NSError *errors) {
    } view:self.view];

}
- (void)addViews{
    [self addLeftItem];
    _row = 0;
    _stat = @"0";
    _rowHeight = 54;
    _nameArray = @[@"请选择原因",@"退款金额"];
    _array = @[@"忘记用优惠券喽",@"突然有事很不好意思",@"重新选定项目",@"需要换个美容师",@"手一哆嗦多拍了"];
    _totalPrcie = @"¥ 0.00";
    _bottomView = [[RMMCommonBottomView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 64 - 49, kScreenWideth, 49)];
    _bottomView.backgroundColor = kWhiteColor;
    [_bottomView.bottomBut setTitle:@"提交申请" forState:(UIControlStateNormal)];
    BUTTON_S(_bottomView.bottomBut, buttonClick);
    [self.view addSubview:_bottomView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64- 49) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = NO;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 0)];
    _tableView.backgroundColor = kCColor;
    [_tableView registerClass:[RMMMineSetTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [_tableView registerClass:[RMMOrderShuoMingTableViewCell class] forCellReuseIdentifier:@"Cells"];
    [self.view addSubview:_tableView];
    
    _pickerView = [[RMMSelcetPickerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    BUTTON_S(_pickerView.rihthBut, doneButClick);
    _pickerView.pickerView.delegate = self;
    _pickerView.pickerView.dataSource = self;
    _pickerView.type = @"location";
    _pickerView.hidden = YES;
    [self.view addSubview:_pickerView];
}
- (void)addLeftItem{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(cancel)];
    self.navigationItem.leftBarButtonItem = item;
   // item.tintColor = COLORFROMCODE(0x9B9B9B1, 1);
    
}
- (void)cancel{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)addTuiKuanDetailsViewWith:(NSString *)type{
    _tuiKuaiJieView = [[RMMTuiKuaiJieGuoView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    [self.view addSubview:_tuiKuaiJieView];

    if ([type isEqualToString:@"1"]) {
        _tuiKuaiJieView.time = @"time";
    }

}
- (void)buttonClick{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"order_id"] = _order_id;
    parDic[@"returnreason"] = _array[_row];
    parDic[@"returnemak"] = _textView.text;
    parDic[@"returnatm"] = _totalPrcie;
    NSString *url = TuiOrderUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [MMProgressHUD show];
            [MMProgressHUD dismissWithSuccess:@"退款申请提交成功!" title:nil afterDelay:1.0];
            NS_POST(@"ORDERUPDATA", nil, nil);
            NS_POST(@"SHOPFRESH", nil, nil);
            // 提交申请
            _tableView.hidden = YES;
            _bottomView.hidden = YES;
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:(UIBarButtonItemStylePlain) target:self action:nil];;
            [self addRightItem];
            [self addTuiKuanDetailsViewWith:@"1"];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
    } error:^(NSError *errors) {
    } view:self.view];
}
- (void)addRightItem{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:(UIBarButtonItemStylePlain) target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = item;

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3) {
        return _rowHeight;
    }
    return 54;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        RMMMineSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.titleName.text = @"请选择原因";
        cell.titleName.textColor = COLORFROMCODE(0x303137, 1);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.content.text = _yuanYin;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;

    } else if (indexPath.section == 2) {
        RMMMineSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.titleName.text = @"退款金额";
        cell.titleName.textColor = COLORFROMCODE(0x303137, 1);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //cell.content.textAlignment = NSTextAlignmentLeft;
        cell.content.text = [NSString stringWithFormat:@"¥ %@",_totalPrcie];
        [RMMFileManager changeLabelTextColorOneWithLabel:cell.content forRange:NSMakeRange(2, _totalPrcie.length) byColor:kBasicColor];
        return cell;

    } else if (indexPath.section == 3){
        RMMOrderShuoMingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cells"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textF.delegate = self;
        cell.textView.delegate = self;
        _textView = cell.textView;
        cell.stat = _stat;
        cell.tag  = 10;
        return cell;
    } else {
        RMMMineSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.titleName.text = @"退款须知";
        cell.titleName.textColor = COLORFROMCODE(0x303137, 1);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 16)];
    view.backgroundColor = kCColor;
    if (section == 3) {
        view.frame = CGRectMake(0, 0, kScreenWideth, 44);
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, view.width - 30, 30)];
        label.text = [NSString stringWithFormat:@"最多可退%@元",_totalPrcie];
        label.font = kFont(13);
        label.textColor = COLORFROMCODE(0x9B9B9B, 1);
        [view addSubview:label];
    }
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 3) {
        return 44;
    }
    return 16;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            _pickerView.hidden = NO;
            [UIView animateWithDuration:kInterval animations:^{
                _pickerView.view.frame = CGRectMake(0, kScreenHeight - 64 - 249, kScreenWideth, 249);
                _pickerView.backView.alpha = 1.0;
            } completion:^(BOOL finished) {
                
            }];
        }
    } else if (indexPath.section == 0) {
        RMMAboutMineViewController *VC = [[RMMAboutMineViewController alloc] init];
        VC.titleName = @"订单退改规则";
        [self.navigationController pushViewController:VC animated:YES];
    }
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    _stat = @"1";
    _rowHeight = 154;
    [_tableView reloadData];
    return YES;
}
- (void)bottomButOff{
    _bottomView.bottomBut.backgroundColor = RGBA(237, 238, 239, 1);
    _bottomView.bottomBut.userInteractionEnabled = NO;
    [_bottomView.bottomBut setTitleColor:RGBA(212, 213, 214, 1) forState:(UIControlStateNormal)];

}
- (void)bottomButOpen{
    _bottomView.bottomBut.backgroundColor = kBasicColor;
    _bottomView.bottomBut.userInteractionEnabled = YES;
    [_bottomView.bottomBut setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
 
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@"最多200字"]) {
        textView.text = @"";
        textView.textColor = COLORFROMCODE(0x303137, 1);
    } else if ([textView.text isEqualToString:@""]) {
        textView.text = @"最多200字";
        textView.textColor = COLORFROMCODE(0xD7D7D7, 1);
    }
    return YES;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _array.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return _array[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _row = row;
    
}

- (void)doneButClick{
    [UIView animateWithDuration:kInterval animations:^{
        _pickerView.view.frame = CGRectMake(0, kScreenHeight - 64, kScreenWideth, 249);
        _pickerView.backView.alpha = 0.0;
    } completion:^(BOOL finished) {
        _pickerView.hidden = YES;
        _yuanYin = _array[_row];
        [self bottomButOpen];
        [_tableView reloadData];

    }];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma 退款
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
